package team.archai.nexus.boot.platform.core.web;

import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.websocket.AuthenticationException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import team.archai.nexus.boot.auth.exception.*;
import team.archai.nexus.boot.constant.HttpEnum;
import team.archai.nexus.boot.web.exception.SecureException;
import team.archai.nexus.boot.web.response.ResponseModel;
import team.archai.nexus.boot.web.response.ResultUtil;

/**
 * @description: 自定义异常处理
 * @author: lishibin
 * @create: 2020-06-27 20:25
 **/
@RestControllerAdvice
@Slf4j
public class WebExceptionHandler {

    /**
     * @program: TinyDustExceptionHandler
     * @description: redis 连接异常
     * @author: lishibin
     * @create: 8:28 PM
     **/
//    @ExceptionHandler({PoolException.class, RedisConnectionFailureException.class, RedisConnectionException.class})
//    public ResponseModel<?> handlePoolException(Exception e) {
//        log.error(e.getMessage(), e);
//        return ResultUtil.error("Redis Connection Error!");
//    }

    /**
     * @program: TinyDustExceptionHandler
     * @description: 自定义异常
     * @author: lishibin
     * @create: 8:28 PM
     **/
//    @ExceptionHandler(TinyDustException.class)
//    public ResponseModel<?> handleTinyDustException(TinyDustException e) {
//        log.error(e.getMessage(), e);
//        return ResultUtil.error(e.getMessage());
//    }

    /**
     * @program: TinyDustExceptionHandler
     * @description: 认证失败
     * @author: lishibin
     * @create: 9:39 PM
     **/
    @ExceptionHandler({JwtException.class})
    public ResponseModel<String> handleJwtException(JwtException e) {
        log.error(e.getMessage(), e);
        return ResultUtil.error(HttpEnum.UNAUTHORIZED.getCode(), e.getMessage(), "");
    }

    /**
     * 用户不存在
     *
     * @param e
     * @return
     */
    @ExceptionHandler({UserNotFountException.class})
    public ResponseModel<String> handleUserNotFountException(UserNotFountException e) {
        log.error(e.getMessage(), e);
        return ResultUtil.error(HttpEnum.USER_AUTH_FAILED.getCode(), e.getMessage(), "");
    }

    /**
     * @program: TinyDustExceptionHandler
     * @description: 认证失败
     * @author: lishibin
     * @create: 9:39 PM
     **/
    @ExceptionHandler({AuthenticationException.class})
    public ResponseModel<String> handleAuthenticationException(AuthenticationException e) {
        log.error(e.getMessage(), e);
        return ResultUtil.error(HttpEnum.UNAUTHORIZED.getCode(), e.getMessage(), "");
    }

    /**
     * @program: TinyDustExceptionHandler
     * @description: 安全校验失败
     * @author: lishibin
     * @create: 12:59 AM
     **/
    @ExceptionHandler(SecureException.class)
    public ResponseModel<String> handleSecureException(SecureException e) {
        log.error(e.getMessage(), e);
        return ResultUtil.error(HttpEnum.SECURE_FAILED.getCode(), e.getMessage(), "");
    }

    /**
     * @program: TinyDustExceptionHandler
     * @description: 全局异常
     * @author: lishibin
     * @create: 9:27 PM
     **/
    @ExceptionHandler({Exception.class})
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseModel<?> handleException(Exception e) {
        log.error(e.getMessage(), e);
        return ResultUtil.error();
    }

    /**
     * @program: TinyDustExceptionHandler
     * @description:
     * @author: lishibin
     * @create: 11:53 AM
     **/
    @ExceptionHandler(ExpiredException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public ResponseModel<?> handleExpiredException(ExpiredException e) {
        log.error(e.getMessage(), e);
        return ResultUtil.out(HttpEnum.TOKEN_EXPIRED, e);
    }

    /**
     * @program: TinyDustExceptionHandler
     * @description:
     * @author: lishibin
     * @create: 11:55 AM
     **/
    @ExceptionHandler(InvalidException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public ResponseModel<?> handleInvalidException(InvalidException e) {
        log.error(e.getMessage(), e);
        return ResultUtil.out(HttpEnum.TOKEN_INVALID, e);
    }

    /**
     * @program: TinyDustExceptionHandler
     * @description: 处理认证失败
     * @author: lishibin
     * @create: 4:37 PM
     **/
    @ExceptionHandler(AuthFailException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ResponseModel<String> handleAuthFailException(AuthFailException e) {
        log.error(e.getMessage(), e);
        return ResultUtil.out(HttpEnum.USER_AUTH_FAILED, e);
    }
}
